#!/usr/bin/env python3
import logging
logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
from scapy.all import *
import multiprocessing
import ipaddress
from a1_ping_one import scapy_ping_one
def ping_scan_some(network):
	ip_segment = ipaddress.ip_network(network)
	print(ip_segment)
	ip_process={}#ip与进程对应的字典
	for ip_addr in ip_segment:
		ip_addr = str(ip_addr)
		ping_one = multiprocessing.Process(target=scapy_ping_one,args=(ip_addr,))#启用多进程调用ping_one函数
		ping_one.start()
		ip_process[ip_addr] = ping_one  #建立ip与进程对应关系

	live_ip = []#存储存活的IP地址

	for ip,proce in ip_process.items():
		if proce.exitcode == 3:#在scapy_ping_one函数中3代表，icmp有返回值
			live_ip.append(ip)
		else:
			pass
	return live_ip

if __name__ == "__main__":
	import sys 
	segment = input("请输入要扫描的网段:")
	#segment = sys.argv[1]
	live_ip = ping_scan_some(segment)
	print(live_ip)


